package com.grape.dto;

import com.grape.utils.BigDecimalUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Data
public class CategoryBudgetDTO {

    /** 分类id */
    @ApiModelProperty(value = "分类id")
    private String id;

    /** 分类名称 */
    @ApiModelProperty(value = "分类名称")
    private String name;

    /** 分类预算 */
    @ApiModelProperty(value = "分类预算")
    private BigDecimal budget;

    /** 分类支出 */
    @ApiModelProperty(value = "分类支出")
    private BigDecimal expenses;

    /** 分类余额 */
    @ApiModelProperty(value = "分类余额")
    private BigDecimal balance;

    /** 余额分类比例 */
    @ApiModelProperty(value = "余额分类比例")
    private Integer ratio;

    public void initField() {
        if (budget == null) {
            budget = BigDecimal.ZERO;
        }
        if (expenses == null) {
            expenses = BigDecimal.ZERO;
        }
        budget = budget.setScale(2, RoundingMode.HALF_UP);
        expenses = expenses.setScale(2, RoundingMode.HALF_UP);
        balance = BigDecimalUtil.sub(budget, expenses).setScale(2, RoundingMode.HALF_UP);
        ratio = BigDecimalUtil.mul(BigDecimalUtil.div(BigDecimalUtil.biggerThenZero(balance) ? balance : BigDecimal.ZERO, budget), BigDecimalUtil.ONE_HUNDRED).setScale(0, RoundingMode.HALF_UP).intValue();
    }
}
